[Perl] Testing for EAGAIN / EWOULDBLOCK on a recv

Posted by Robert S. Barnes on Stack Overflow See other posts from Stack Overflow or by Robert S. Barnes
Published on 2010-03-14T14:03:08Z Indexed on 2010/03/14 14:05 UTC
Read the original article Hit count: 387

Filed under:
|
|

I'm testing a socket to see if it's still open:

        my $dummy = '';
        my $ret = recv($sock, $dummy, 1, MSG_DONTWAIT | MSG_PEEK);
        if (!defined $ret || (length($dummy) == 0 
                && $! != EAGAIN && $! != EWOULDBLOCK )) {
            logerr("Broken pipe? ".__LINE__." $!");
        } else {
            # socket still connected, reuse
            logerr(__LINE__.": $!");
            return $sock;
        }

I'm passing this code a socket I know for certain is open and it's always going through the first branch and logging "Broken pipe? 149 Resource temporarily unavailable".

I don't understand how this is happening since "Resource temporarily unavailable" is supposed to correspond to EAGAIN as far as I know.

I'm sure there must be something simple I'm missing. And yes, I know this is not a full proof way to test and I account for that.

© Stack Overflow or respective owner

Related posts about perl

Related posts about tcp